﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business.Operations;
using Entity.Framework;

namespace HPDMAS.UserControls
{
    public partial class StatKayit : System.Web.UI.UserControl
    {
        private string _statAdi;
        private List<string> statNoListesi;
        public int sehirId;
        //private string _sehirAdi;
        private int _statId;
        private string _ilceAdi;
        private int _ilceId;
        private string _statNo;
        private string _statAdresi;
        string sayfaUrl;

        protected void Page_Load(object sender, EventArgs e)
        {
            ddlStatNoDoldur();
            //ddlSehirDoldur();
            ddlIlceDoldur();
        }

        private void ddlStatNoDoldur()
        {
            statNoListesi = new List<string>();
            for (int i = 1; i < 8; i++)
            {
                statNoListesi.Add(i.ToString());
            }
            ddlStatNo.DataSource = statNoListesi;
            ddlStatNo.DataBind();
        }

        //private void ddlSehirDoldur()
        //{
        //    ddlSehir.DataSource = SehirOperation.SehirAdiGetAll();
        //    ddlSehir.DataBind();
        //    ddlSehir.SelectedValue = SehirOperation.SehirAdiGetBySehirId(_sehirId);
        //}

        private void ddlIlceDoldur()
        {
            ddlIlce.DataSource = IlceOperation.IlceAdiGetBySehirId(Convert.ToString(sehirId));
            ddlIlce.DataBind();
        }

        protected void btnStatKayit_Click(object sender, EventArgs e)
        {
            try
            {
                StatEkle();
                Session["kaydedildiMesaji"] = "Stat kaydedildi.";
                sayfaUrl = "~/IHKBaskani/KayitTamamlandi.aspx";
            }
            catch (Exception ee)
            {
                Session["hataMesaji"] = "Hata oluştu.<br />Hata mesajı: " + ee.Message;
                sayfaUrl = "~/HataSayfasi.aspx";
            }
            finally
            {
                Response.Redirect(sayfaUrl);
            }
        }

        private void StatEkle()
        {
            _statAdi = txtStatAdi.Text.Trim();
            //_sehirAdi = ddlSehir.SelectedValue;
            _ilceAdi = ddlIlce.SelectedValue;
            _ilceId = IlceOperation.IlceIdGetByIlceAdiAndSehirId(_ilceAdi, Convert.ToString(sehirId));
            _statNo = ddlStatNo.SelectedValue;
            _statAdresi = txtStatAdresi.Text.Trim();

            Stat stat = new Stat();
            stat.StatAdi = _statAdi;
            stat.StatNo = _statNo;
            stat.StatAdres = _statAdresi;
            stat.DegisiklikTarihi = DateTime.Now;
            StatOperation.StatInsert(stat);

            _statId = StatOperation.StatIdGetByStatAdi(_statAdi);

            StatSehir statSehir = new StatSehir();
            statSehir.StatID = _statId;
            statSehir.SehirID = sehirId;
            statSehir.IlceID = _ilceId;
            StatSehirOperation.StatSehirInsert(statSehir);
        }
    }
}